<template>
	<view class="container">
		<!-- 售后类型 -->
		<view class="type-section">
			<text class="section-title">售后类型</text>
			<view class="type-list">
				<view 
					class="type-item" 
					v-for="(item, index) in afterSalesTypes" 
					:key="index"
					:class="{ active: selectedType === item.id }"
					@click="selectType(item.id)"
				>
					<text class="type-name">{{item.name}}</text>
					<text class="check-icon" v-if="selectedType === item.id">✓</text>
				</view>
			</view>
		</view>
		
		<!-- 问题描述 -->
		<view class="description-section">
			<text class="section-title">问题描述</text>
			<textarea 
				v-model="description" 
				class="description-input" 
				placeholder="请详细描述您遇到的问题（最少10个字）"
				maxlength="500"
			></textarea>
			<text class="word-count">{{description.length}}/500</text>
		</view>
		
		<!-- 图片上传 -->
		<view class="upload-section">
			<text class="section-title">上传图片</text>
			<view class="upload-list">
				<view 
					class="upload-item" 
					v-for="(item, index) in imageList" 
					:key="index"
				>
					<image :src="item" mode="aspectFill" class="preview-img"></image>
					<text class="delete-btn" @click="deleteImage(index)">×</text>
				</view>
				<view class="upload-btn" @click="chooseImage" v-if="imageList.length < 9">
					<text class="upload-icon">+</text>
					<text class="upload-text">上传图片</text>
				</view>
			</view>
			<text class="upload-tip">最多上传9张图片</text>
		</view>
		
		<!-- 联系方式 -->
		<view class="contact-section">
			<text class="section-title">联系方式</text>
			<input 
				type="number" 
				v-model="phone" 
				class="contact-input" 
				placeholder="请输入您的手机号码"
				maxlength="11"
			/>
		</view>
		
		<!-- 提交按钮 -->
		<view class="footer">
			<button class="submit-btn" @click="submitAfterSales">提交申请</button>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'

// 售后类型
const afterSalesTypes = ref([
	{ id: 'refund', name: '仅退款' },
	{ id: 'return', name: '退货退款' },
	{ id: 'exchange', name: '换货' },
	{ id: 'repair', name: '维修' }
])

// 选中的售后类型
const selectedType = ref('')

// 问题描述
const description = ref('')

// 图片列表
const imageList = ref([])

// 联系电话
const phone = ref('')

// 选择售后类型
const selectType = (id) => {
	selectedType.value = id
}

// 选择图片
const chooseImage = () => {
	uni.chooseImage({
		count: 9 - imageList.value.length,
		success: (res) => {
			imageList.value = [...imageList.value, ...res.tempFilePaths]
		}
	})
}

// 删除图片
const deleteImage = (index) => {
	imageList.value.splice(index, 1)
}

// 提交售后申请
const submitAfterSales = () => {
	if (!selectedType.value) {
		uni.showToast({
			title: '请选择售后类型',
			icon: 'none'
		})
		return
	}
	
	if (description.value.length < 10) {
		uni.showToast({
			title: '问题描述不能少于10个字',
			icon: 'none'
		})
		return
	}
	
	if (!phone.value) {
		uni.showToast({
			title: '请输入联系电话',
			icon: 'none'
		})
		return
	}
	
	if (!/^1[3-9]\d{9}$/.test(phone.value)) {
		uni.showToast({
			title: '请输入正确的手机号码',
			icon: 'none'
		})
		return
	}
	
	uni.showLoading({
		title: '提交中...'
	})
	
	// 这里可以添加提交售后申请的接口调用
	setTimeout(() => {
		uni.hideLoading()
		uni.showToast({
			title: '提交成功',
			icon: 'success'
		})
		
		// 返回订单列表
		setTimeout(() => {
			uni.redirectTo({
				url: '/pages/order/list'
			})
		}, 1500)
	}, 2000)
}
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #F5F5F5;
	padding-bottom: 120rpx;
}

/* 售后类型样式 */
.type-section {
	background-color: #FFFFFF;
	padding: 30rpx;
	margin-bottom: 20rpx;
}

.section-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 20rpx;
	display: block;
}

.type-list {
	display: flex;
	flex-wrap: wrap;
	margin: 0 -10rpx;
}

.type-item {
	width: calc(50% - 20rpx);
	height: 80rpx;
	margin: 10rpx;
	background-color: #F5F5F5;
	border-radius: 10rpx;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 30rpx;
}

.type-item.active {
	background-color: #FFF0F0;
}

.type-name {
	font-size: 28rpx;
	color: #333;
}

.check-icon {
	font-size: 32rpx;
	color: #D02B29;
}

/* 问题描述样式 */
.description-section {
	background-color: #FFFFFF;
	padding: 30rpx;
	margin-bottom: 20rpx;
	position: relative;
}

.description-input {
	width: 100%;
	height: 200rpx;
	font-size: 28rpx;
	color: #333;
	line-height: 1.5;
}

.word-count {
	position: absolute;
	right: 30rpx;
	bottom: 30rpx;
	font-size: 24rpx;
	color: #999;
}

/* 图片上传样式 */
.upload-section {
	background-color: #FFFFFF;
	padding: 30rpx;
	margin-bottom: 20rpx;
}

.upload-list {
	display: flex;
	flex-wrap: wrap;
	margin: 0 -10rpx;
}

.upload-item {
	width: 200rpx;
	height: 200rpx;
	margin: 10rpx;
	position: relative;
}

.preview-img {
	width: 100%;
	height: 100%;
	border-radius: 10rpx;
}

.delete-btn {
	position: absolute;
	top: -20rpx;
	right: -20rpx;
	width: 40rpx;
	height: 40rpx;
	line-height: 40rpx;
	text-align: center;
	background-color: rgba(0, 0, 0, 0.5);
	color: #FFFFFF;
	border-radius: 50%;
	font-size: 32rpx;
}

.upload-btn {
	width: 200rpx;
	height: 200rpx;
	margin: 10rpx;
	background-color: #F5F5F5;
	border-radius: 10rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.upload-icon {
	font-size: 60rpx;
	color: #999;
	margin-bottom: 10rpx;
}

.upload-text {
	font-size: 24rpx;
	color: #999;
}

.upload-tip {
	font-size: 24rpx;
	color: #999;
	margin-top: 20rpx;
	display: block;
}

/* 联系方式样式 */
.contact-section {
	background-color: #FFFFFF;
	padding: 30rpx;
	margin-bottom: 20rpx;
}

.contact-input {
	font-size: 28rpx;
	color: #333;
}

/* 底部提交按钮样式 */
.footer {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: #FFFFFF;
	display: flex;
	align-items: center;
	padding: 0 30rpx;
}

.submit-btn {
	width: 100%;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	background-color: #D02B29;
	color: #FFFFFF;
	border-radius: 40rpx;
	font-size: 32rpx;
}
</style> 